package com.c.ctgproject.ui.collect

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityPartWeightBinding
import com.c.ctgproject.entity.guang.PartsInWarehouseEntity
import com.c.ctgproject.event.PositionEvent
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import com.google.gson.Gson
import com.lwkandroid.imagepicker.ImagePicker
import com.lwkandroid.imagepicker.data.ImageBean
import com.lwkandroid.widget.ninegridview.NineGridBean
import org.greenrobot.eventbus.EventBus
import java.io.File

/**
 *作者:LiBW
 *创建日期: 2021/9/10
 *描述:散件过磅 页面 (零部件)
 */
class PartWeightActivity : BaseActivityV2() {
    private val mBinding by lazy { ActivityPartWeightBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private val mOrderId by lazy { intent.getIntExtra(MyConstants.ORDER_ID, -1) }
    private val imageResultCode = 100
    private val imageShowList = ArrayList<String>()
    private val mListPosition by lazy { intent.getIntExtra(MyConstants.POSITION, -1) }
    private val mOrderIdList by lazy {
        intent.getStringExtra(MyConstants.ORDER_ID_LIST)
    }

    override fun event() {
        mBinding.mPhotoNGV.init(this, imageShowList, 1, imageResultCode)
        ApiMethods.apiSubscribe(ApiMethods.retrofit.blukPartDetail(mOrderId.handlerNull()), MyObserverHandlerLoading(this, loading) {
            MyLog.d("散件入库详情回调:$it")
            val data = getGson().fromJson(it, PartsInWarehouseEntity::class.java).data
            mBinding.poundNo.setText(data?.poundNo.handlerNull())
            mBinding.poundWeight.setText(data?.poundWeight.handlerNull())
            mBinding.purchasePoundNo.setText(data?.purchasePoundNo)
            mBinding.purchaseWeight.setText(data?.purchaseWeight.handlerNull())
            mBinding.mRemarkET.setText(data?.poundRemark)
            data?.poundPicture?.let {
                val resultList: MutableList<NineGridBean> = ArrayList()
                val nineGirdData = NineGridBean(it)
                imageShowList.add(it)
                resultList.add(nineGirdData)
                mBinding.mPhotoNGV.addDataList(resultList)
            }
        })
    }

    override fun click() {
        mBinding.mConfirmBTN.setOnClickListener {

                onHttp(imageShowList[0])

        }


//            val poundWeight = mBinding.poundWeight.text.handlerNull()
//            val poundNo = mBinding.poundNo.text.handlerNull()
//            val purchaseWeight = mBinding.purchaseWeight.text.handlerNull()
//            val purchasePoundNo = mBinding.purchasePoundNo.text.handlerNull()
//            when {
//                poundWeight.isEmpty() -> showToast("请输入过磅重量")
//                poundNo.isEmpty() -> showToast("请输入磅单号")
//                else -> {
//                    val map = HashMap<String, Any>()
//                    map["orderId"] = mOrderId
//                    map["weight"] = poundWeight
//                    map["poundNo"] = poundNo
//                    map["purchaseWeight"] = purchaseWeight
//                    map["purchasePoundNo"] = purchasePoundNo
//                    ApiMethods.apiSubscribe(ApiMethods.retrofit.partWeight(handlerBody(map)), MyObserverHandlerLoading(this, loading) {
//                        showToast("提交过磅成功")
//                        finish()
//                    })
//                }
//            }
    }

    private fun onHttp(imageUrl: String? = null) {
        val poundNo = mBinding.poundNo.text.handlerNull()
        val poundWeight = mBinding.poundWeight.text.handlerNull()
        val purchasePoundNo = mBinding.purchasePoundNo.text.handlerNull()
        val purchaseWeight = mBinding.purchaseWeight.text.handlerNull()
        val poundRemark = mBinding.mRemarkET.text.handlerNull()
        when {
            poundNo.isEmpty() -> {
                showToast("请输入磅单号")
                mBinding.mNumberLL.myShake()
            }
            poundWeight.isEmpty() -> {
                showToast("请输入磅单重量")
                mBinding.mWeightLL.myShake()
            }
            else -> {
                val map = HashMap<String, Any>()
                if (mOrderId != -1) {
                    map["orderId"] = mOrderId
                }

                map["orderIdList"] = Gson().fromJson(mOrderIdList, ArrayList<Int>()::class.java)
                map["poundNo"] = poundNo
                map["poundWeight"] = poundWeight
                if (purchasePoundNo.isNotEmpty()) map["purchasePoundNo"] = purchasePoundNo
                if (purchaseWeight.isNotEmpty()) map["purchaseWeight"] = purchaseWeight
                if (poundRemark.isNotEmpty()) map["poundRemark"] = poundRemark
                if (imageUrl != null && imageUrl.isNotEmpty()) {
                    map["poundPicture"] = imageUrl
                }

                ApiMethods.apiSubscribe(ApiMethods.retrofit.partWeight(handlerBody(map)), MyObserverHandlerLoading(this, loading) {
                    MyLog.d("提交回调:$it")
                    showToast("提交成功")
                    //EventBus.getDefault().post(PositionEvent(mListPosition))
                    setResult(1)
                    finish()
                })
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        data?.let {
            when (requestCode) {
                //图片回调
                imageResultCode -> {
                    val list: List<ImageBean>? = data.getParcelableArrayListExtra(ImagePicker.INTENT_RESULT_DATA)
                    list?.let {
                        if(list.size>0){
                            val file = File(list[0].imagePath)
                            //图片压缩
//                val newFile: File = CompressHelper.getDefault(this).compressToFile(file)
//                val bitmap = BitmapFactory.decodeFile(newFile.absolutePath)
//                val bitmaResult = ImageShuiYinUtil.createWaterMaskRightBottom(this, bitmap, "广汽内场管理系统", 10, 10)
//                val  ff=saveBitmapFile(this,bitmaResult,1)
//                val ff=ImageUtil.imageWaterMark(this,file,1)
                            val ff: File = WaterRemark.handlerWaterRemark2File(this, file, "1")


                            upDataFile(arrayListOf(ff)) {
                                if (it != null && it.isNotEmpty()) {
                                    //onHttp(it[0].fileUrl.handlerNull())
                                    val resultList: MutableList<NineGridBean> = ArrayList()
                                    it[0].fileUrl?.let {
                                        val nineGirdData = NineGridBean(it)
                                        imageShowList.add(it)
                                        resultList.add(nineGirdData)
                                        mBinding.mPhotoNGV.addDataList(resultList)
                                    }

                                } else {
                                    showToast("图片上传异常请稍后再试")
                                }


                            }

                        }
                    }


                }
                else -> {

                }
            }
        }
    }

}

